package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.client.ItemServiceClient;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RequestMapping("/api/item")
@RestController
public class ItemApiController {

    @Autowired
    private ItemServiceClient itemServiceClient;

    @Autowired
    private ListFeignClient listFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;//线程池

    @ApiOperation(value = "数据汇总")
    @GetMapping("/{skuId}")
    public Map<String, Object> getItem(@PathVariable Long skuId) {
        Map map = new HashMap();
        CompletableFuture<SkuInfo> skuInfo1 = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = itemServiceClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryView = skuInfo1.thenAcceptAsync((skuInfo) -> {//then继续执行run执行新的
            BaseCategoryView baseCategoryView = itemServiceClient.getBaseCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);//exceptionally()专门处理异常

        CompletableFuture<BigDecimal> price = CompletableFuture.supplyAsync(() -> {
            BigDecimal skuPrice = itemServiceClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
            return skuPrice;
        }, threadPoolExecutor);

        CompletableFuture<Void> spuSaleAttrList = skuInfo1.thenAcceptAsync((skuInfo) -> {
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = itemServiceClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJson = skuInfo1.thenAcceptAsync((skuInfo) -> {
            Map<String, String> skuValueIdsMap = itemServiceClient.getSkuValueIdsMap(skuInfo.getSpuId());
            map.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));
        }, threadPoolExecutor);

        //添加热点
        CompletableFuture.runAsync(()->{
            listFeignClient.hotScore(skuId);
        },threadPoolExecutor);

        CompletableFuture.allOf(skuInfo1, valuesSkuJson, spuSaleAttrList, price, categoryView).join();//等待所有的线程完成
        return map;

    }


}
